<?php

require('config.php');
header('Content-type: text/html; charset=utf-8'); // en-tête HTTP
if(isset($_GET['id']) and $_GET['id']<>''){
$page = new rootLayoutPrint();
$translate = tmxTranslate('place_detail.xml', REP_XLAT_TEMPLATE);
$tpl_file='create_report_pdf.tpl';
$create_report_pdf = new flyLayout(REP_TPL . $tpl_file);
$create_report_pdf->start();
$create_report_pdf->setVariable('http_host',HTTP_HOST);
$language_link=$_GET['id'];
$language_code = $_SESSION['language_code'] = isset($_GET['language'])?$_GET['language']:(isset($_SESSION['language_code'])?$_SESSION['language_code']:language_default);
$location = modelLocationMap::getListPdf($language_link,1,2);//$status =2,3,6 
	if($location){
		$page->setBaliseTitle($language_link);
		
			//$create_report_pdf->setVariable('place_name',($location->name_place));
			if($location->content!=''){
				$create_report_pdf->setVariable('full_description', nl2br(substr($location->content,0,225)));
				$create_report_pdf->showBlock('show_full_description');
			}
			if($location->reference!=''){
				$create_report_pdf->setVariable('reference', ($location->reference));
				$create_report_pdf->showBlock('show_reference');
			}
			$all_price = modelMap::getPrice($location->id_map_address_price);
			if($all_price->price_rental<>0){
				$create_report_pdf->setVariable('price_rental', number_format($all_price->price_rental,0,'.',' ').' '.CURRENCY);
				$create_report_pdf->showBlock('price_rental');
			}
			if($all_price->price_sell<>0){
				$create_report_pdf->setVariable('price_sell', number_format($all_price->price_sell,0,'.',' ').' '.CURRENCY);
				$create_report_pdf->showBlock('price_sell');
			}
			$getTypeReal=modelRealEstate::getLocationMapRealEstateSelect($language_link,$language_code);
			$getCategory=modelMap::getListCategoryTestSelect($language_link,$language_code);
			$title_estate = $getTypeReal->title ;
			$create_report_pdf->setVariable('real_title', htmlspecialchars($title_estate));
			if($getCategory->size()>0){
			foreach($getCategory as $key => $category) {
				$create_report_pdf->setVariable('category_title',$category->title.'');
				$create_report_pdf->parseList('list_category');
			}
			}else{
				$create_report_pdf->setVariable('category_title','');
				$create_report_pdf->parseList('list_category');			
			}
			// Main Photo 
			$photo_main = modelPhoto::getMainPhotoPdf($language_link);
			if($photo_main){
				$url_image = $photo_main->url_repertoire.$photo_main->nom_serveur;
				if(file_exists($url_image)){
					$create_report_pdf->setVariable('photo_main', $url_image);
				}else{
					$create_report_pdf->setVariable('photo_main', NOIMAGE);
				}
			}	
			$listPhotos_sub=modelPhoto::getListPhotoLocation_submainPhoto($language_link);
			if($listPhotos_sub->size()==1){
					$value = 1;
					$format = modelUploadFormat::getObject(FORMAT_PREVIEW_PDF_BIG);	
				}
			if($listPhotos_sub->size() > 0) {
				foreach($listPhotos_sub as $key => $fichier){
					$url_image = $fichier->url_repertoire.$fichier->nom_serveur;
					
					if(file_exists($url_image)){
						$create_report_pdf->setVariable('sub_main', $url_image);
					}else{
						$create_report_pdf->setVariable('sub_main', NOIMAGE);
					}
					$create_report_pdf->parseList('list_photo_sub_main');
				}
				
			}
						
			$create_report_pdf->showBlock("have_value");
	}else{$create_report_pdf->showBlock("no_value");}
$create_report_pdf->stop();
$string1 = "
<html>
<head>
<meta http-equiv='Content-Language' content='".$location->language_code."' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<meta http-equiv='Content-Style-Type' content='text/css' />
<style>
	body {
	    background: none repeat scroll 0 0 transparent !important;
	    font-family: Arial,Helvetica,Garuda,sans-serif;
	    margin: 0px;
	}

div.wrap_detail{width:700px;position:fixed;height:50px;top:160px;}
.show_price{font-size:30px;}
.show_reference{font-size:30px;}
#main_print{border-top:1px solid #33378C;border-left:1px solid #33378C;border-bottom:1px solid #33378C;border-right:none;margin-left:-20px;}
</style>
</head>
<body>
";
$string2 = "</body></html>";
$pdf = $string1.$create_report_pdf.$string2;
if($_SESSION['language_code']<>'kh'){
	if($location){
		 $Handle = fopen('template_pdf.txt', 'w');
		 fwrite($Handle, $pdf); 
		 
		 //////mb_convert_encoding('text_here','utf-16','utf-8');
		 require_once("dompdf/dompdf_config.inc.php");
		  ob_start();
		//$key = md5($_SESSION['username'].ENCRYPTION_KEY);
		$myFile = "template_pdf.txt";
		  require_once ($myFile);
		  $html_message = ob_get_contents();
		  ob_end_clean();
		  if ( get_magic_quotes_gpc() )
		    $_POST["html"] = stripslashes($html_message);
		  $dompdf = new DOMPDF();
		  $dompdf->load_html($html_message);
		  $dompdf->set_paper('a4', 'landscape');
		  $dompdf->render();		  
		  
		  $pdfoutput = $dompdf->output();
		  //Write file to folder
		  if(!is_dir("pdf")){
		  	mkdir("pdf", 0777);
		  }
		  $Handle = fopen('pdf/'.$location->language_link.'-'.removeAccents($location->name_place).'-'.$location->language_code.'.pdf', 'w');
		  fwrite($Handle, $pdfoutput); 
		  //file_put_contents('pdf/'.$location->language_link.'-'.removeAccents($location->name_place).'-'.$location->language_code.'.pdf', $pdfoutput);
		  $id_pdf = modelLocationMap::getPdfProperty($location->language_link)->id_pdf;
		  if($id_pdf==0){
			  //Save file to server
			  	$nom_initial = addslashes($location->name_place.'.pdf');
				$key_unique = time() . rand(10000, 99999);
				$nom_telechargement = addslashes($location->language_link.'-'.removeAccents($location->name_place).'-'.$location->language_code.'.pdf');
			  	$nom_serveur = addslashes(removeAccents($location->name_place).'.pdf');
				$id_repertoire = ID_REP_PDF;
				$type = 'application/pdf';
				$size =  filesize('pdf/'.$location->language_link.'-'.removeAccents($location->name_place).'-'.$location->language_code.'.pdf');
				$balise_alt = '';
				$title = '';
				$author = '';
				$description = '';
			  	//echo $nom_initial.','. $nom_telechargement.','.  $nom_telechargement.','.  $id_repertoire.','.  $type.','.  $size.','.  $balise_alt.','.  $key_unique.','.  $title.','.  $author.','.  $description;die();
			  	//$id_upload = modelUploadFile::insert($nom_initial, $nom_telechargement, $nom_telechargement, $id_repertoire, $type, $size, $balise_alt, $key_unique, $title, $author, $description);
			  	$id_upload = modelLocationMap::insertUpload($nom_telechargement, $nom_telechargement, $nom_telechargement, $id_repertoire, $type, $size, $balise_alt, $key_unique, $title, $author, $description);
			  	modelLocationMap::updatePdfProperty($location->language_link,$id_upload);
		  }else{
		 	$fichier = modelLocationMap::getImageDetail($id_pdf);
		 	if($fichier){
			  	$nom_initial = $location->name_place.'.pdf';
				$nom_telechargement = $location->language_link.'-'.removeAccents($location->name_place).'-'.$location->language_code.'.pdf';
				$nom_serveur = removeAccents($location->name_place).'.pdf';
			  	modelLocationMap::updateIdPdf($id_pdf,$nom_telechargement,  $nom_telechargement,$nom_telechargement);
		 	}else{
			  //Save file to server
			  	$nom_initial = $location->name_place.'.pdf';
				$key_unique = time() . rand(10000, 99999);
				//$nom_telechargement = removeAccents($location->name_place).'-'.$key_unique.'.pdf';
				$nom_telechargement = $location->language_link.'-'.removeAccents($location->name_place).'-'.$location->language_code.'.pdf';
			  	$nom_serveur = removeAccents($location->name_place).'.pdf';
				$id_repertoire = ID_REP_PDF;
				$type = 'application/pdf';
				$size =  filesize('pdf/'.$location->language_link.'-'.removeAccents($location->name_place).'-'.$location->language_code.'.pdf');
				$balise_alt = '';
				$title = '';
				$author = '';
				$description = '';
			  	$id_upload = modelUploadFile::insert($nom_telechargement, $nom_telechargement, $nom_telechargement, $id_repertoire, $type, $size, $balise_alt, $key_unique, $title, $author, $description);
			  	modelLocationMap::updatePdfProperty($location->language_link,$id_upload);
		 		
		 	}
		  }
		  //fclose($fp);
		  exit(0);
	}
}
  ///////
$page->setBody($create_report_pdf);
$page->display();
}
?>